// pages/returnProduct/returnProduct.js
const common = require("../../utils/conmon.js");
var app = getApp();
Page({
  data: {
    typeHidden: true,
    return1: true,
    return2: false,
    problem1: true,
    problem2: false,
    problem: 0
  },
  problem1: function () {
    this.setData({
      problem1: true,
      problem2: false,
      problem: 0
    })
    // console.log(this.data.problem)
  },
  problem2: function () {
    this.setData({
      problem1: false,
      problem2: true,
      problem: 1
    })
    // console.log(this.data.problem)
  },
  return1: function () {
    this.setData({
      return1: true,
      return2: false,
      reasonType: 1
    })
  },
  change1: function () {
    this.setData({
      return1: false,
      return2: true,
      reasonType: 2
    })
  },
  onShareAppMessage: function (res) {
    return {
      title: '成都皮肤美容微整',
      path: '/pages/homepage/homepage?id=' + wx.getStorageSync("userid"),
    }
  },
  onLoad: function (options) {//从订单页面拿到当前退货订单的id与orderformid
    // 页面初始化 options为页面跳转所带来的参数
    //   app.returenProductId=e.target.dataset.id
    // app.returenProductorderformid=e.target.dataset.orderformid;
    // console.log(app.returenProductOrderFormId)
    // console.log(app.returenProductorderformid.indexOf(0))
    var superType = app.returenProductorderformid.indexOf(0)
    if (superType == "z") {//判断它是超市订单还是直营订单，超市订单没有换货，直营有换货功能
      this.setData({
        typeHidden: false
      })
    }
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },

  setUserName: function (e) {//储存退货姓名在当前页面以供传到后台
    if (e.detail.value != "") {
      this.setData({
        setUserName: e.detail.value
      })
    }
  },

  setUserPhone: function (e) {//储存退货手机号在当前页面以供传到后台
    if (e.detail.value != "") {
      this.setData({
        setUserPhone: e.detail.value
      })
    }
  },


  setUserAdvice: function (e) {//储存退货原因在当前页面以供传到后台
    if (e.detail.value != "") {
      this.setData({
        setUserAdvice: e.detail.value
      })
    }
  },

  returnSubmit: function (e) {//提交退货信息 
    console.log(app.paymentstypes)
    var that = this;
    if (that.data.setUserAdvice && that.data.setUserName && that.data.setUserPhone) {
      if (true) {//app.paymentstypes == 1判断支付方式，如果是1就是货到付款，如果为2就是在线支付
        wx.request({
          url: app.globalData.allUrl + "/orderFormAction/returnsOrderForm",
          data: {
            id: app.returenProductId,
            remark: that.data.setUserAdvice,
            returnCause: that.data.problem,
            userName: that.data.setUserName,
            phone: that.data.setUserPhone
          },
          method: 'GET',
          // header: {}, // 设置请求的 header
          success: function (res) {
            // success
            if (res.data) {
              if (res.data == "已退货") {
                wx.showToast({
                  title: '已退货',
                  icon: 'success',
                  duration: 2000
                })
                setTimeout(function () {
                  wx.navigateBack({
                    delta: '1?id=2', // 回退前 delta(默认为1) 页面
                  })
                }, 2000)
              } else {
                wx.showToast({
                  title: '提交成功',
                  icon: 'success',
                  duration: 2000
                })
                app.returnOrderFormDetailNumber = 2;
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1, // 回退前 delta(默认为1) 页面
                    success: function (res) {
                      // success             
                    }
                  })
                }, 2000)

              }
            } else {
              wx.showToast({
                title: '提交失败',
                icon: 'success',
                duration: 2000
              })
            }
          }
        })}
      // } else if (app.paymentstypes == 2) {
      //   console.log("在线支付退货");
      //   wx.request({
      //     url: app.globalData.allUrl + "/refundAction/transferPay",
      //     data: {
      //       TransferNo: app.returnorderformids,
      //       // userId:wx.getStorageSync('userid')
      //     },
      //     method: 'GET',
      //     // header: {}, // 设置请求的 header
      //     success: function (res) {
      //       // success
      //       wx.showToast({
      //         title: res.data.msg,
      //         icon: 'success',
      //         duration: 2000
      //       })
      //       app.returnOrderFormDetailNumber = 2;
      //       setTimeout(function () {
      //         wx.navigateBack({
      //           delta: 1, // 回退前 delta(默认为1) 页面
      //           success: function (res) {
      //             // success             
      //           }
      //         })
      //       }, 2000)
      //     }
      //   })
      // }
    } else {
      wx.showToast({
        title: '请填写完整退货内容',
        icon: 'success',
        duration: 2000
      })
    }

  }
})

